<template>
  <el-row class="page">
    <el-row v-for="(item,index) in $store.state.selectedInterface.data.params" :key="item.id">
      <el-col :span="6">
        <el-input placeholder="key" size="small" :value="item.key" @input="(value)=>changeHeader('key',index,value)"></el-input>
      </el-col>
      <el-col :span="6" style="padding:0 10px"
        ><el-input placeholder="value" size="small" :value="item.value" @input="(value)=>changeHeader('value',index,value)"></el-input
      ></el-col>
      <el-col :span="6"
        ><el-input placeholder="text" size="small" :value="item.text" @input="(value)=>changeHeader('text',index,value)"></el-input
      ></el-col>
      <el-col :span="4" style="padding-left: 10px;">
        <el-button
          @click="addParams(index)"
          size="mini"
          type="primary"
          icon="el-icon-plus"
        />
        <el-button
        v-if="index!==0"
          @click="removeParams(index)"
          size="mini"
          type="danger"
          icon="el-icon-delete"
        />
      </el-col>
    </el-row>
  </el-row>
</template>

<script>
import _ from 'lodash'
export default {
  data() {
    return {
      defaultParams: {
        key: "",
        value: "",
        text: ""
      },
      list: []
    };
  },
  components: {},
  computed: {},
  created() {},
  mounted() {

  },
  watch: {},
  methods: {
    addParams(index) {
      let params = _.cloneDeep(this.$store.state.selectedInterface.data.params)
      params.splice(index+1, 0, this.defaultParams)
      this.$store.commit('setSelectedInterface',_.cloneDeep({
        ...this.$store.state.selectedInterface,
        data:{
          ...this.$store.state.selectedInterface.data,
          params,
        }
      }))
    },
    removeParams(index){
      let params = _.cloneDeep(this.$store.state.selectedInterface.data.params)
      params.splice(index, 1)
      this.$store.commit('setSelectedInterface',_.cloneDeep({
        ...this.$store.state.selectedInterface,
        data:{
          ...this.$store.state.selectedInterface.data,
          params,
        }
      }))
    },
    changeHeader(fieldName,index,value){
      let params = _.cloneDeep(this.$store.state.selectedInterface.data.params)
      params[index][fieldName] = value
      this.$store.commit('setSelectedInterface',_.cloneDeep({
        ...this.$store.state.selectedInterface,
        data:{
          ...this.$store.state.selectedInterface.data,
          params,
        }
      }))
    }
  }
};
</script>

<style scoped></style>
